﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ESpace
{
    /// <summary>делегат для фии-события нажатия кнопки OK при редактировании</summary>
    /// <param name="RetValue">отредактированное значение</param>
    /// <returns>Удачно ли редактирование. (нужно ли закрывать форму редактирования)</returns>
    public delegate bool ConfirmClickDelegate(string RetValue);

    partial class FormEditValue : Form
    {
        ConfirmClickDelegate Confirm_Click = null;

        public FormEditValue()
        {
            InitializeComponent();
        }

        private void FormEditValue_Shown(object sender, EventArgs e)
        {
            txbValue.SelectAll();
            txbValue.Focus();
        }

        public void ShowDialog(string InitialValue, ConfirmClickDelegate Confirm_Click)
        {
            txbValue.Text = InitialValue;
            this.Confirm_Click = Confirm_Click;
            ShowDialog();
        }

        public void ShowDialogPosInCursor(string InitialValue, ConfirmClickDelegate Confirm_Click)
        {
            StartPosition = FormStartPosition.Manual;
            Top = Cursor.Position.Y - Height / 2;
            Left = Cursor.Position.X - Width / 2;
            if (Top < 0) Top = 0;
            if (Left < 0) Left = 0;

            ShowDialog(InitialValue, Confirm_Click);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if(!Confirm_Click(txbValue.Text)) return;
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void txbValue_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.IsInputKey = true;
                btnOK_Click(null, null);
            }
            if (e.KeyCode == Keys.Escape)
            {
                btnCancel_Click(null, null);
            }
        }

    }
}
